#!/usr/bin/perl
#target_mod.cgi
require './tgtman-lib.pl';

&ReadParse();
my $devmark = $in{'imark'};
my $err_msg;
my $tname = $in{'tname'};
my $dev = $in{'hiddev'};
my $user = $in{'tuser'};
my $pwd = $in{'tpwd'};
my $mark = $in{'setaccount'};
my @tlist=&get_target_list();
my $found = 0; my $founddev = 0;
#my $tg; 
#m $err_msg;
if(!&checkname($tname)){
	$err_msg = $text{'invalid_tname'};
	goto ERROR_OUT;
}
foreach $t (@tlist) {
	if($t->{'name'} eq $tname) {
		$found = 1;
#		$tg = $t;
	}
	if($t->{'device'} eq $dev) {
		$founddev = 1;
	}
}
if($found){
	$err_msg = $text{'err_tname_used'};
	goto ERROR_OUT;	
}
if(!&checkdev($dev))
{
	$err_msg = $text{'invalid_tdev'};
	goto ERROR_OUT;
}

if($founddev){
	$err_msg = $text{'err_dev_used'};
	goto ERROR_OUT;
}
if($dev =~ /hda/){
	$err_msg = $text{'err_dev_sysvol'};
	goto ERROR_OUT;
}
my @devstat = &vol_mount_status($dev);
if($devstat[0]){
        $err_msg = $text{'err_dev_fs'};
        goto ERROR_OUT;
}
if($mark eq 'y'){
	if(!&checkuser($user))
	{
		$err_msg = $text{'invalid_tuser'};
		goto ERROR_OUT;
	}
}	
if($user){

	if(!&checkpwd($pwd))
	{
		$err_msg = $text{'invalid_tpwd'};
		goto ERROR_OUT;
	}
}
&crt_target($tname, $dev, $user, $pwd, $mark, $devmark);
#goto ERROR_OUT;
$rc = `/etc/init.d/iscsi-target status`;
if($rc =~ /running/){
        `/etc/init.d/iscsi-target restart`;
}
&redirect("index.cgi");
ERROR_OUT:
&error_setup("");
&error($err_msg);
